<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="比特币数量" prop="bitcoin">-->
<!--        <el-input-->
<!--          v-model="queryParams.bitcoin"-->
<!--          placeholder="请输入比特币数量"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="usd">-->
<!--        <el-input-->
<!--          v-model="queryParams.usd"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="creditsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="account" align="center" prop="accountName" />
      <el-table-column label="bitcoin" align="center" prop="bitcoin" />
      <el-table-column label="amount(usd)" align="center" prop="usd" />
      <el-table-column label="recharge voucher" align="center" prop="rechargeFile">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleWatch(scope.row)"
          >watch</el-button>
        </template>
      </el-table-column>
      <el-table-column label="status" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">unreviewed</span>
          <span v-if="scope.row.status == 1">reviewed</span>
        </template>
      </el-table-column>
      <el-table-column label="option" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-if="scope.row.status == 0"
          >audit</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改比特币充值凭证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <img :src="imgPath" class="thumbnail">
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">OK</el-button>
        <el-button @click="cancel">Cancel</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCredits, addCredits, updateCredits, recharge } from "@/api/system/credits";
import { getFileInfo } from "@/api/cms/fileInfo";

export default {
  name: "Credits",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 比特币充值凭证表格数据
      creditsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        bitcoin: null,
        usd: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      url2: process.env.VUE_APP_BASE_API,
      imgPath: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查看充值图片
    handleWatch(row) {
      var that = this
      if (row.rechargeFile) {
        getFileInfo(row.rechargeFile).then(response => {
          that.imgPath = that.url2 + response.data.filePath
          that.open = true;
          that.title = "recharge voucher";
        });
      } else {
        this.$message.warning('Not Found Recharge Voucher！');
      }
    },
    /** 查询比特币充值凭证列表 */
    getList() {
      this.loading = true;
      listCredits(this.queryParams).then(response => {
        this.creditsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        account: null,
        bitcoin: null,
        usd: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAudit(row) {
      console.log(row.id)
      recharge({id: row.id, account: row.account}).then(res => {
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCredits(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCredits(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
<style scoped>
.thumbnail {
  width: 100%;
  height: 100%;
}
</style>
